<?php
/*
 * 标题：阿里紫订单管理系统
 * 作者：忆天网络 QQ715457186
 * 官方网址：www.fjd100.com||www.yi66.nett
 */
class VersionAction extends Action {

    public function index(){
		header('content-type:text/html;charset=utf-8');
		$ItemTemplate = M('ItemTemplate');
		$fields = $ItemTemplate->query('desc __TABLE__ `extend`');
		if(empty($fields)){  $ItemTemplate->query("ALTER TABLE __TABLE__ add `extend` text  after redirect_uri");  }
		$fields = $ItemTemplate->query('desc __TABLE__ `theme`');
		if(empty($fields)){  $ItemTemplate->query("ALTER TABLE __TABLE__ add `theme` varchar(25)  after id");  }

		$Item = M('Item');
		$Item->query('alter table __TABLE__ modify column params_type varchar(20);');
		$fields = $Item->query('desc __TABLE__ `thumb`');
		if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `thumb` varchar(255)  after image"); }
		$fields = $Item->query('desc __TABLE__ `sms_send`');
		if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `sms_send` text after send_content"); }
		$fields = $Item->query('desc __TABLE__ `timer`');
		if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `timer` int(1) NOT NULL DEFAULT  '0'  after sms_send"); }
		$fields = $Item->query('desc __TABLE__ `user_id`');
		if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `user_id` int(12) NOT NULL DEFAULT  '1'  after id");}
		$fields = $Item->query('desc __TABLE__ `seller_id`');
		if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `seller_id` int(12) NOT NULL DEFAULT  '1'  after user_pid"); }
		$fields = $Item->query('desc __TABLE__ `link_pay_url`');
		if(empty($fields)){ 
			$Item->query("ALTER TABLE __TABLE__ add `link_pay_url` varchar(255) after timer"); 
			$Item->query("ALTER TABLE __TABLE__ add `link_pay_info` varchar(255) after link_pay_url");
		}
        $fields = $Item->query('desc __TABLE__ `quantity`');
        if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `quantity` int(12) NOT NULL DEFAULT  '1000'  after price"); }
        $fields = $Item->query('desc __TABLE__ `click`');
        if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `click` int(12) NOT NULL DEFAULT  '1'  after timer"); }
		$fields = $Item->query('desc __TABLE__ `params_name`');
        if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `params_name` varchar(25)  after sort_order"); }
		$fields = $Item->query('desc __TABLE__ `buy_num`');
        if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `buy_num` varchar(100)  after click"); }
		$fields = $Item->query('desc __TABLE__ `buy_num_decrease`');
        if(empty($fields)){ $Item->query("ALTER TABLE __TABLE__ add `buy_num_decrease` varchar(100)  after buy_num"); }

        $Order = M('Order');
		$rs = $Order->query('alter table __TABLE__ modify column channel_id varchar(20);');
		$rs = $Order->query('alter table __TABLE__ modify column `item_extends` text;');
		$fields = $Order->query('desc __TABLE__ `is_sent`');
		if(empty($fields)){ $Order->query("ALTER TABLE __TABLE__ add `is_sent`  tinyint(1) NOT NULL DEFAULT  '0'  after is_delete"); }
		$fields = $Order->query('desc __TABLE__ `order_page`');
		if(empty($fields)){ $Order->query("ALTER TABLE __TABLE__ add `order_page` varchar(15) NOT NULL DEFAULT 'single' after order_no");}
		$fields = $Order->query('desc __TABLE__ `user_pid`');
		if(empty($fields)){  $Order->query("ALTER TABLE __TABLE__ add `user_pid` int(12) NOT NULL DEFAULT '1' after user_id"); }
		$fields = $Order->query('desc __TABLE__ `province`');
		if(empty($fields)){  $Order->query("ALTER TABLE __TABLE__ add `province` varchar(20) after region"); }
		$fields = $Order->query('desc __TABLE__ `city`');
		if(empty($fields)){  $Order->query("ALTER TABLE __TABLE__ add `city` varchar(20) after province"); }
		$fields = $Order->query('desc __TABLE__ `area`');
		if(empty($fields)){  $Order->query("ALTER TABLE __TABLE__ add `area` varchar(20) after city"); }
        $fields = $Order->query('desc __TABLE__ `qudao`');
        if(empty($fields)){
            $Order->query("ALTER TABLE __TABLE__ add `qudao` varchar(30) after update_time");
            $Order->query("ALTER TABLE __TABLE__ add `qudaonum` varchar(30) after qudao");
        }
        $fields = $Order->query('desc __TABLE__ `coupon`');
        if(empty($fields)){
            $Order->query("ALTER TABLE __TABLE__ add `coupon` varchar(12) after update_time");
        }
		$fields = $Order->query('desc __TABLE__ `item_sn`');
        if(empty($fields)){
            $Order->query("ALTER TABLE __TABLE__ add `item_sn` varchar(25) after item_id");
        }
		
		$User = M('User');
		$fields = $User->query('desc __TABLE__ `pid`');
		if(empty($fields)){ $User->query("ALTER TABLE __TABLE__ add `pid` int(12) NOT NULL DEFAULT  '0'  after id");}
		$fields = $User->query('desc __TABLE__ `pid`');
		$rs = $User->query("alter table __TABLE__ modify column role enum('admin','member','agent') NOT NULL DEFAULT 'admin'");
		$fields = $User->query('desc __TABLE__ `group_id`');
		if(empty($fields)){ $User->query("ALTER TABLE __TABLE__ add `group_id` int(12) NOT NULL DEFAULT  '0'  after pid");}
		
		$Comments = M('Comments');
		$Comments->query('alter table __TABLE__ modify column add_time date NOT NULL;');
		$fields = $Comments->query('desc __TABLE__ `region`');
		if(empty($fields)){ $Comments->query("ALTER TABLE __TABLE__ add `region` varchar(100) NOT NULL DEFAULT  ''  after name"); }
		$fields = $Comments->query('desc __TABLE__ `reply_content`');
		if(empty($fields)){ $Comments->query("ALTER TABLE __TABLE__ add `reply_content` text after content"); }
		$fields = $Comments->query('desc __TABLE__ `status`');
		if(empty($fields)){ $Comments->query("ALTER TABLE __TABLE__ add `status` tinyint(1) NOT NULL DEFAULT '1' after name"); }
		$fields = $Comments->query('desc __TABLE__ `mobile`');
		if(empty($fields)){ $Comments->query("ALTER TABLE __TABLE__ add `mobile` varchar(15) NOT NULL DEFAULT  '' after name"); }
		$fields = $Comments->query('desc __TABLE__ `title`');
		if(empty($fields)){ $Comments->query("ALTER TABLE __TABLE__ add `title` varchar(50) DEFAULT  '' after status"); }
		$fields = $Comments->query('desc __TABLE__ `start`');
		if(empty($fields)){ $Comments->query("ALTER TABLE __TABLE__ add `start` varchar(20) NOT NULL DEFAULT  'heart-5' after reply_content"); }
		else{ $Comments->query("ALTER TABLE __TABLE__ modify column `start` varchar(20) NOT NULL DEFAULT  'heart-5'");}
		
		$Model = M();
		$prefix = C('DB_PREFIX');
		$Model->query("CREATE TABLE IF NOT EXISTS `{$prefix}code` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,`mobile` varchar(16) NOT NULL,`item_id` int(12) NOT NULL DEFAULT '0',`code` varchar(10) NOT NULL,`type` tinyint(4) NOT NULL DEFAULT '1',`status` tinyint(1) NOT NULL DEFAULT '0',`add_time` int(10) NOT NULL,PRIMARY KEY (`id`),KEY `mobile` (`mobile`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
		$Model->query("CREATE TABLE IF NOT EXISTS `{$prefix}user_group` (`id` int(12) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(25) NOT NULL,`discount` tinyint(3) NOT NULL DEFAULT '100',`auth` text,PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");
		$Model->query("CREATE TABLE IF NOT EXISTS `{$prefix}user_logs` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,`username` varchar(25) NOT NULL,`types` varchar(20) NOT NULL,`content` text,`add_ip` varchar(15) DEFAULT NULL,`add_time` datetime DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8");
		
		$UserGroup = M('UserGroup');
		$fields = $UserGroup->query('desc __TABLE__ `role`');
		if(empty($fields)){ $UserGroup->query("ALTER TABLE __TABLE__ add `role` enum('admin','agent') NOT NULL DEFAULT 'agent' after id"); }
		
		$Category = M('Category');
		$fields = $Category->query('desc __TABLE__ `image`');
		if(empty($fields)){ $Category->query("ALTER TABLE __TABLE__ add `image` varchar(255) after sort_order"); }
		
		
		$Model->query("INSERT INTO `alizi_setting` VALUES ('html_file', 'Html/')");
		$Model->query("INSERT INTO `alizi_setting` VALUES ('DEFAULT_LANG', 'zh-cn')");
		$Model->query("INSERT INTO `alizi_setting` VALUES ('export_type', 'excel')");
		$Model->query("INSERT INTO `alizi_setting` VALUES ('export_order', '[\"order_no\",\"item_name\",\"item_params\",\"quantity\",\"total_price\",\"name\",\"mobile\",\"province\",\"city\",\"area\",\"address\",\"payment\",\"status\",\"remark\",\"add_time\"]')");
		$Model->query("INSERT INTO `alizi_setting` VALUES ('header_nav', '商品分类||index.php?m=Index&a=category\r\n订单查询||index.php?m=Index&a=query\r\n关于我们||index.php?m=Index&a=detail&id=1')");
		$Model->query("INSERT INTO `alizi_setting` VALUES ('footer_nav', '商品分类||index.php?m=Item&a=category||menu\r\n订单查询||index.php?m=Item&a=query||newspaper')");
		
		$this->redirect('Version/update');
    }
	
	function update(){
		header('content-type:text/html;charset=utf-8');
		$Order = M('Order');
		$Item = M('Item');
		
		$regions = $Order->field('id,region')->select();
		foreach($regions as $reg){
			if(!empty($reg['region'])){
				list($data['province'],$data['city'],$data['area'])=explode(' ',$reg['region']);
				$Order->where(array('id'=>$reg['id']))->save($data);
			}
		}
		
		$orderList = $Order->field('id,item_id')->select();
		foreach($orderList as $li){
			$item_sn = $Item->where('id='.$li['item_id'])->getField('sn');
			$Order->where('id='.$li['id'])->setField('item_sn',$item_sn);
		}
		
		delFiles('./Public/Cache/');
		die('升级完成');
	}
}	